<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="classSelectFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="班级名称" prop="classesName">
          <el-input
            v-model="queryParams.classesName"
            placeholder="请输入班级名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="班级编码" prop="classesCode">
          <el-input
            v-model="queryParams.classesCode"
            placeholder="请输入班级编码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="学校名称" prop="schoolId">
          <el-select
            v-model.trim="queryParams.schoolId"
            clearable
            filterable
            placeholder="请选择学校"
            @change="changeSchool"
            class="!w-240px"
          >
            <el-option
              v-for="item in schoolList"
              :key="item.id"
              :label="item.schoolName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="机构名称" prop="collegeId">
          <el-select
            v-model.trim="queryParams.collegeId"
            clearable
            filterable
            placeholder="请选择机构"
            @keyup.enter="handleQuery"
            class="!w-240px"
          >
            <el-option
              v-for="item in collegeList"
              :key="item.id"
              :label="item.collegeName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年级" prop="grade">
          <el-select
            v-model="queryParams.grade"
            placeholder="请选择状态"
            clearable
            class="!w-240px"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.CMIS_GRADE_TYPE)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap :setPadding="false">
      <el-table v-loading="loading" :data="list" @select="select" ref="dataTable">
        <el-table-column type="selection" width="55" />
        <el-table-column label="班级名称" prop="classesName" show-overflow-tooltip />
        <el-table-column label="班级编码" prop="classesCode" show-overflow-tooltip />
        <el-table-column label="学校名称" prop="schoolName" show-overflow-tooltip />
        <el-table-column label="机构名称" prop="collegeName" show-overflow-tooltip />
        <el-table-column
          label="创建时间"
          prop="createTime"
          :formatter="dateFormatter"
          show-overflow-tooltip
        />
        <el-table-column label="操作" fixed="right" width="100">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['cmis:classes:update']"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['cmis:classes:delete']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="submitEnable">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('选择班级') // 弹窗的标题
import * as ClassesApi from '@/api/cmis/classes'
import * as SchoolApi from '@/api/cmis/school'
import * as CollegeApi from '@/api/cmis/college'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  classesName: null,
  classesCode: null,
  schoolId: null,
  collegeId: null,
  grade: null
})
const classSelectFormRef = ref() // 搜索的表单
const schoolList = ref([]) //学校列表
const collegeList = ref([]) //机构列表
const submitEnable = ref(true) //是否可以提交

const changeSchool = async (value: any) => {
  queryParams.collegeId = null
  getCollegeList(value)
}

const getCollegeList = async (schoolId: string) => {
  const res = await CollegeApi.getCollegeList({ schoolId })
  collegeList.value = res
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ClassesApi.getClassesPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  classSelectFormRef.value?.resetFields()
  handleQuery()
}

/** 打开弹窗 */
const open = async () => {
  resetQuery()
  // 加载学校列表
  schoolList.value = await SchoolApi.getSchoolList('')
  dialogVisible.value = true
}

const emit = defineEmits(['data'])
const submitForm = async () => {
  emit('data', dataTable.value.getSelectionRows())
  dialogVisible.value = false
}

const dataTable = ref()
const select = async (selection, row) => {
  // 没选值不能确认处理
  submitEnable.value = dataTable.value.getSelectionRows().length <= 0

  // 单选处理
  if (props.isSingleSelect) {
    dataTable.value.clearSelection()
    if (selection.length == 0) return
    dataTable.value.toggleRowSelection(row, true)
  }
}

const props = defineProps({
  isSingleSelect: Boolean
})

defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
